/**
 * 报表设计公司(2011)
 */
package net.abbp.report.view.cell.painter;

import java.util.HashMap;
import java.util.Map;

import net.abbp.report.model.ReportModel;
import net.abbp.report.view.cell.value.ImageValue;


/**
 * 单元格绘制器管理器
 * 
 * @version 1.0 2011-3-22
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public enum CellPainterManager {
    INSTANCE;

    // 绘制器缓存
    private Map<Class<?>, ICellPainter> painterMap = new HashMap<Class<?>, ICellPainter>();

    CellPainterManager() {
        // 注册通用绘制器
        painterMap.put(Object.class, new DefaultCellPainter());
        // 注册模型绘制器
        painterMap.put(ReportModel.class, new ReportCellPainter());
        // 注册图像绘制器
        painterMap.put(ImageValue.class, new ImageCellPainter());
    }

    /**
     * 根据类获取绘制器
     * 
     * @param clazz
     *            类
     * @return 绘制器
     */
    public ICellPainter getCellPainter(Class<?> clazz) {
        ICellPainter editor = painterMap.get(clazz);

        if (editor == null) {
            return painterMap.get(Object.class);
        } else {
            return editor;
        }
    }

    /**
     * 根据对象获取绘制器
     * 
     * @param o
     *            对象
     * @return 绘制器
     */
    public ICellPainter getCellPainter(Object o) {
        Class<?> clazz = (o == null ? Object.class : o.getClass());

        return this.getCellPainter(clazz);
    }
}
